package com.tianruan.dc.modules.sys.user.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianruan.dc.modules.sys.user.entity.db.UserRoleDO;
import com.tianruan.dc.modules.sys.user.mapper.UserRoleMapper;
import com.tianruan.dc.modules.sys.user.service.IUserRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("userRoleService")
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleDO> implements IUserRoleService {


    @Autowired
    private UserRoleMapper userRoleMapper;

    @Transactional
    public void insertUserRole(String userId, List<String> roleIdList){
        userRoleMapper.deleteByUserId(userId);
        if (CollectionUtils.isNotEmpty(roleIdList)) {
            roleIdList.stream()
                    .map(roleId->new UserRoleDO().setUserId(userId).setRoleId(roleId))
                    .forEach(userRole->userRoleMapper.insert(userRole));
        }

    }
}
